<html>
<body>
	<table border="1">
		<tr>
			<td valign="top">
				<%-- -------- Include menu HTML code -------- --%> <jsp:include
					page="index.html" />
			</td>
			<td><%@ page language="java" import="java.sql.*,java.util.*"%>
				<%
					Connection conn = null;
					try {
						// Load Oracle Driver class file
						DriverManager
								.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());

						// Make a connection to the MS SQL database "cse132b"
						conn = DriverManager
								.getConnection(
										"jdbc:sqlserver://localhost\\SQLEXPRESS;databaseName=cse132b",
										"cse132b", "cse132b");
	
						String action = request.getParameter("action");
						// Check if an insertion is requested
						if (action != null && action.equals("insert")) {
							// Begin transaction
							conn.setAutoCommit(false);
							
							PreparedStatement pstmt = conn
									.prepareStatement("INSERT INTO degree VALUES (?, ?, ?, ?, ?)");
							pstmt.setString(1, request.getParameter("student_id"));
							pstmt.setString(2, request.getParameter("field_of_study"));
							pstmt.setString(3, request.getParameter("level"));
							pstmt.setString(4, request.getParameter("university"));
							pstmt.setInt(5, Integer.parseInt(request.getParameter("year")));
							
							pstmt.executeUpdate();
							
							// Commit transaction
							conn.commit();
							conn.setAutoCommit(true);
						}
						// Check if an update is requested
						if (action != null && action.equals("update")) {
							// Begin transaction
	                        conn.setAutoCommit(false);
	                     	
							PreparedStatement pstmt = conn.prepareStatement("UPDATE degree SET field_of_study = ?, " + 
									"level = ?, university = ?, year = ? WHERE degree_id = ?");
							pstmt.setString(1, request.getParameter("field_of_study"));
							pstmt.setString(2, request.getParameter("level"));
							pstmt.setString(3, request.getParameter("university"));
							pstmt.setInt(4, Integer.parseInt(request.getParameter("year")));
							pstmt.setInt(5, Integer.parseInt(request.getParameter("degree_id")));
							pstmt.executeUpdate();
	                     	
							// Commit transaction
	                        conn.commit();
	                        conn.setAutoCommit(true);
						}
						
						// Check if a delete is requested
	                    if (action != null && action.equals("delete")) {
	                    	// Begin transaction
	                        conn.setAutoCommit(false);
	                        
	                        // Create the prepared statement and use it to
	                        // DELETE the student FROM the committee table.
	                        PreparedStatement pstmt = conn.prepareStatement(
	                            "DELETE FROM degree WHERE degree_id = ?");
	                        pstmt.setInt(1, Integer.parseInt(request.getParameter("degree_id")));
	                        pstmt.executeUpdate();

	                        // Commit transaction
	                        conn.commit();
	                        conn.setAutoCommit(true);
	                    }
						
						// Create the statement
						Statement statement = conn.createStatement();
						
						// Use the created statement to SELECT
						// the grad student attributes.
						SortedMap<String, String> students = new TreeMap<String, String>();
						ResultSet rsGrad = statement.executeQuery("SELECT student_id, first_name, last_name FROM student");
						while (rsGrad.next()) {
							String info = rsGrad.getString("first_name") + " " + rsGrad.getString("last_name");
							students.put(rsGrad.getString("student_id"), info);
						}
						
						// SELECT degree
						ResultSet rs = statement.executeQuery("SELECT * FROM degree");
				%>
				<table border="1">
					<tr>
						<th>Student</th>
						<th>Field of Study</th>
						<th>Level</th>
						<th>University</th>
						<th>Year</th>
					</tr>
					<tr>
						<form action="degree.jsp" method="POST">
							<input type="hidden" value="insert" name="action">
							<th>
								<select name="student_id" style="width:200px">
								<%
									for (Map.Entry<String, String> student : students.entrySet()) {
								%>
						 			<option value="<%= student.getKey() %>">
						 				<%= student.getValue() + " (" + student.getKey() + ")" %>
						 			</option>
								<%
									}
								%>
								</select>
							</th>
							<th><input value="" name="field_of_study" size="15"></th>
							<th><input value="" name="level" size="10"></th>
							<th><input value="" name="university" size="15"></th>
							<th><input value="" name="year" size="10"></th>
							<th><input type="submit" value="Insert"></th>
						</form>
					</tr>
					<%
						// Iterate over the ResultSet
						while (rs.next()) {
					%>
					<tr>
                        <form action="degree.jsp" method="POST">
                        	<input type="hidden" value="update" name="action">
                        	<input type="hidden" value="<%= rs.getString("degree_id") %>" name="degree_id">
                        	<td>
                        		<input type="hidden" value="<%= rs.getString("student_id") %>" name="student_id">
                        		<%= students.get(rs.getString("student_id")) + " (" + rs.getString("student_id") + ")" %>
                        	</td>
                        	<td><input value="<%= rs.getString("field_of_study") %>" name="field_of_study" size="15"></td>
							<td><input value="<%= rs.getString("level") %>" name="level" size="10"></td>
							<td><input value="<%= rs.getString("university") %>" name="university" size="15"></td>
							<td><input value="<%= rs.getString("year") %>" name="year" size="10"></td>
                        	<td>
                                <input type="submit" value="Update">
                            </td>
                        </form>
                        <form action="degree.jsp" method="get">
                            <input type="hidden" value="delete" name="action">
                            <input type="hidden" 
                                value="<%= rs.getString("degree_id") %>" name="degree_id">
                            <td>
                                <input type="submit" value="Delete">
                            </td>
                        </form>
                    </tr>
                <%
						}
                %>
				<%
					} catch (SQLException e) {
						System.out.println(e.getMessage());
						conn.rollback();
					} catch (Exception e) {
						System.out.println(e.getMessage());
						conn.rollback();
					}
				%>
				</table>
			</td>
		</tr>
	</table>
</body>
</html>